<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-20
 * @package TinPhp
 */
if (!defined('IN_TINPHP')) {
    exit('Access Denied');
}
require_once("DB.php");

/** 数据Map类，用户为数据库增、改使用的对象 */
class TinDataMap {

    private $data;
    private $types;

    public function TinDataMap() {
        $this->data = array();
    }
    /** 分配对象 
     * @param Map $zxData map对象
     */
    public function assign($zxData) {
        $this->data = $zxData->data;
        $this->types = $zxData->types;
    }
    /** 添加指定key、val值对 
     * @param string $key 标识
     * @param string $value 值
     * @param string $type DB枚举类型 @see DB 
     */
    public function addEntry($key, $value, $type) {
        $this->data[$key] = $value;
        $this->types[$key] = $type;
    }
    /** 添加设置key、val值对
     * @param string $key 标识
     * @param string $value 值
     *  */
    public function setValue($key, $value) {
        $this->data[$key] = $value;
    }
    /** 获取Map中对应KEY的值
     * @param string $key 标识 */
    public function getValue($key) {
        return $this->data[$key];
    }
    /** 获取Map中对应KEY的DB枚举类型
     * @param string $key 标识 */
    public function getType($key) {
        return $this->types[$key];
    }
    /** 获取Map中所有KEY
     * @return array */
    public function &getKeys() {
        return array_keys($this->data);
    }
    /** 获取Map中所有Value
     * @return array */
    public function getValues() {
        return array_values($this->data);
    }
    /** 获取Map中所有类型
     * @return array */
    public function getTypes() {
        return array_values($this->types);
    }
//    public function mergeWith($other) {
//        
//    }
    /** 检测Map对象中的值与类型是否匹配
     * @return bool */
    public function checkTypeValid() {
        $keys = &$this->getKeys();
        foreach ($keys as $key) {
            $value = $this->data[$key];
            $dbType = $this->types[$key];
            
            switch ($dbType) {
                case DB::BOOL:
                    if (!is_bool($value))
                        return false;
                    break;

                case DB::INT:
                case DB::UNSIGNED_INT:
                case DB::TINYINT:
                case DB::UNSIGNED_TINYINT:
                case DB::SMALLINT:
                case DB::UNSIGNED_SMALLINT:
                case DB::MEDIUMINT:
                case DB::UNSIGNED_MEDIUMINT:
                    if (!is_int($value))
                        return false;
                    break;
                case DB::BIGINT:
                case DB::UNSIGNED_BIGINT:
                // TODO:

                case DB::FLOAT:
                case DB::UNSIGNED_FLOAT:
                case DB::DOUBLE:
                case DB::UNSIGNED_DOUBLE:
                    if (!is_float($value))
                        return false;
                    break;
                case DB::DECIMAL:
                case DB::UNSIGNED_DECIMAL:
                // TODO:

                case DB::CHAR:
                case DB::VARCHAR:
                case DB::ENUM:
                    if (!is_string($value))
                        return false;
                    break;
                case DB::TEXT:
                case DB::BLOB:
                case DB::CLOB:

                //case DB::DATE:
                case DB::DATETIME:
                //case DB::TIMESTAMP:
                //case DB::TIME:
                //case DB::YEAR:

                default:
                //return false;
            }
        }
        return true;
    }

    /** debug获取map对象 */
    public function getData() {
        return $this->data;
    }

}

?>
